/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.cm;

import static cc.aileron.wsgi.container.WsgiHttpMethodType.GET;
import static cc.aileron.wsgi.container.WsgiHttpMethodType.POST;
import static cc.aileron.wsgi.container.utility.WsgiProcessRegistry.registry;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.template.exception.CompileException;
import cc.aileron.web.cm.auth.AuthProcessProvider;
import cc.aileron.web.cm.auth.form.FormAuthProcess;
import cc.aileron.web.cm.dto.AuthFormDto;
import cc.aileron.web.cm.process.MessagePostProcess;
import cc.aileron.web.cm.process.MessageViewerProcess;
import cc.aileron.web.cm.resource.IndexResource;
import cc.aileron.web.cm.resource.MessageListResource;
import cc.aileron.web.cm.resource.MessagePostResource;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.WsgiSetting;
import cc.aileron.wsgi.container.WsgiContainer;
import cc.aileron.wsgi.provider.WsgiRedirectProvider;
import cc.aileron.wsgi.provider.WsgiTemplateProvider;

/**
 * Processの設定を行います
 * @author Aileron
 *
 */
public class ProcessSetting implements WsgiSetting
{
    /**
     * contentType
     */
    private final String contentType ;
    
    /**
     * auth-process-provider
     */
    private final AuthProcessProvider authProcessProvider;
    
    /**
     * reidrect-provider
     */
    private final WsgiRedirectProvider redirectProcessProvider;
    
    /**
     * index-template
     */
    private final WsgiProcess<IndexResource> indexTemplate;
    
    /**
     * form-auth-process
     */
    private final FormAuthProcess formAuthProcess;
    
/* message viewer */
    
    /**
     * message-viewer
     */
    private final MessageViewerProcess messageViewerProcess;
    
    /**
     * message-viwer-template
     */
    private final WsgiProcess<MessageListResource> messageViewerTemplate;
    
    /**
     * message-post
     */
    private final MessagePostProcess messagePostProcess;
    
    /**
     * construct
     * @param authProcess
     * @param formAuthProcess
     * @throws ResourceNotFoundException 
     * @throws CompileException 
     */
    public ProcessSetting(
            final ApplicationContext context,
            final AuthProcessProvider authProcessProvider,
            final WsgiRedirectProvider redirectProvider,
            final WsgiTemplateProvider templateProvider,
            final FormAuthProcess formAuthProcess,
            final MessageViewerProcess messageViewerProcess,
            final MessagePostProcess messagePostProcess) 
    throws CompileException, ResourceNotFoundException
    {
        /*
         * setting
         */
        this.contentType = "text/html; charset=UTF-8";
        
        /*
         * provider
         */
        this.redirectProcessProvider = redirectProvider;
        this.authProcessProvider = authProcessProvider;
        
        /*
         * process
         */
        this.formAuthProcess = formAuthProcess;
        this.messageViewerProcess = messageViewerProcess;
        this.messagePostProcess = messagePostProcess;
        
        /*
         * template
         */
        this.indexTemplate = templateProvider.get("Resource/index.xhtml", contentType);
        this.messageViewerTemplate = templateProvider.get("Resource/message.xhtml", contentType);
    }

    /* (非 Javadoc)
     * @see cc.aileron.wsgi.WsgiSetting#setting(cc.aileron.wsgi.container.WsgiContainer)
     */
    @Override
    public void setting(WsgiContainer container)
    {
        registry(IndexResource.class,"")
            .uri("/index")
            .method(GET)
            .add(indexTemplate)
            .register(container);
        
        registry(AuthFormDto.class,"mail,pass")
            .uri("/auth-form.post")
            .method(POST)
            .add(formAuthProcess)
            .add(redirectProcessProvider.<AuthFormDto>get("/message.html"))
            .register(container);
        
        registry(MessageListResource.class,"")
            .uri("/message.html")
            .method(GET)
            .add(authProcessProvider.<MessageListResource>get())
            .add(messageViewerProcess)
            .add(messageViewerTemplate)
            .register(container);
        
        registry(MessagePostResource.class,"")
            .uri("/message.html")
            .method(POST)
            .add(authProcessProvider.<MessagePostResource>get())
            .add(messagePostProcess)
            .add(redirectProcessProvider.<MessagePostResource>get("/message.html"))
            .register(container);
    }
}